import datetime
import json
import os


def get_farm_items():
    return {
        'popberry': {
            'shear': 'Basic Shears',
            'item_name': 'Popberry',
            'seed_name': 'Popberry Seeds',
            'energy_cost': 1.5,
            'min_price': 1,
            'wait_seconds': 65
        },
        'grainbow': {
            'shear': 'Basic Shears',
            'item_name': 'Grainbow',
            'seed_name': 'Grainbow Seeds',
            'energy_cost': 1.5,
            'min_price': 20,
            'wait_seconds': 65
        },
        'orange_grumpkin': {
            'shear': 'Basic Shears',
            'item_name': 'Orange Grumpkin',
            'seed_name': 'Orange Grumpkin Seeds',
            'energy_cost': 1.5,
            'min_price': 20,
            'wait_seconds': 65
        },
        'clover': {
            'shear': 'Bronzenite Shears',
            'item_name': 'Clover',
            'seed_name': 'Clover Seeds',
            'energy_cost': 2.5,
            'min_price': 20,
            'wait_seconds': 95
        },
        'tato': {
            'shear': 'Bronzenite Shears',
            'item_name': 'Tato Fruit',
            'seed_name': 'Tato Seeds',
            'energy_cost': 2.5,
            'min_price': 20,
            'wait_seconds': 95
        },
        'java': {
            'shear': 'Bronzenite Shears',
            'item_name': 'Java Berry',
            'seed_name': 'Java Seeds',
            'energy_cost': 2.5,
            'min_price': 20,
            'wait_seconds': 95
        },
        'muck': {
            'shear': 'Bronzenite Shears',
            'item_name': 'Muckchuck',
            'seed_name': 'Muckchuck Seeds',
            'energy_cost': 2.5,
            'min_price': 20,
            'wait_seconds': 95
        },
        'napricot': {
            'shear': 'Irontite Shears',
            'item_name': 'Napricot',
            'seed_name': 'Napricot Seeds',
            'energy_cost': 2.5,
            'min_price': 20,
            'wait_seconds': 125
        },
        'slothmato': {
            'shear': 'Irontite Shears',
            'item_name': 'Slothmato',
            'seed_name': 'Slothmato Seeds',
            'energy_cost': 2.5,
            'min_price': 20,
            'wait_seconds': 125
        },
        'butterberry': {
            'shear': 'Adamaxium Shears',
            'item_name': 'Butterberry',
            'seed_name': 'Butterberry Seeds',
            'energy_cost': 2.5,
            'min_price': 20,
            'wait_seconds': 155
        }
    }


def get_cooking_items():
    return {
        'popberry_jam': {
            'name': 'Popberry Jam',
            'sell_item_name': 'Popberry Jam',
            'min_price': 10,
            'energy_cost': 8,
            'items': {
                'Popberry': 2,
            },
            'wait_seconds': 2 * 60
        },
        'popberry_jam_12': {
            'name': 'Barrel of Popberry Jam',
            'sell_item_name': 'Popberry Jam',
            'min_price': 10,
            'energy_cost': 96,
            'items': {
                'Popberry': 12 * 2,
            },
            'wait_seconds': 25 * 60
        },
        'syrup_12': {
            'name': 'Barrel of Syrup',
            'sell_item_name': 'Syrup',
            'min_price': 10,
            'energy_cost': 96,
            'items': {
                'Sap': 12 * 2,
            },
            'wait_seconds': 25 * 60
        },
        'popberry_loaf': {
            'name': 'Popberry Loaf',
            'sell_item_name': 'Popberry Loaf',
            'min_price': 10,
            'energy_cost': 14,
            'items': {
                'Popberry Jam': 3,
                'Grainbow Flour': 2,
            },
            'wait_seconds': 4 * 60 + 5
        },
        'grumpkin_loaf': {
            'name': 'Blue Grumpkin Loaf',
            'sell_item_name': 'Blue Grumpkin Loaf',
            'min_price': 10,
            'energy_cost': 2,
            'items': {
                'Blue Grumpkin Puree': 3,
                'Grainbow Flour': 2,
            },
            'wait_seconds': 24
        }
    }


def get_no_price_item_names():
    return [
        'Jumebara Head',
        'Popberry Wine', 'Grainshine', 'Blue Grumpkin Wine',
        # cooking
        'Grainbow Tart','Popberry Pie',  'Popberry Loaf', 'Clover Fruit Pie', 'Napricot Pie','Java Pie', 'Muckchuck Pie', 'Tato Hash', 'Tato Scramble',
        'Orange Grumpkin Pie', 'Blue Grumpkin Pie', 'Blue Grumpkin Loaf', 'Clover Fruit Pie', 'Slice of Slothmato Pizza',
        'Cubecumber Loaf'
    ]

def get_items():
    try:
        path = os.path.join(os.path.dirname(os.path.abspath(__file__)), '../data/items.json')
        with open(path, 'r') as f:
            items = json.load(f)
    except json.JSONDecodeError:
        items = {}
    except FileNotFoundError:
        items = {}
    return items

def get_cooking_item(key):
    return get_cooking_items().get(key)


def get_sell_items(user_id):
    sell_items = {}
    # 烹饪产品
    for cooking_item in get_cooking_items().values():
        sell_items[cooking_item.get('sell_item_name')] = cooking_item.get('min_price')
    return sell_items


def get_accounts():
    return get_env('accounts', {})


def get_account(user_id):
    return get_accounts().get(str(user_id), {})


def is_vip(user_id):
    return get_account(user_id).get('is_vip', False)


def can_trade(user_id):
    return get_account(user_id).get('can_trade', True)


def get_item_keep_qty(user_id, item_key):
    item = get_farm_items().get(item_key, {})
    keep_qty = item.get('keep', 1)
    account = get_account(user_id)
    if 'keep' in account:
        keep = account.get('keep', {})
        if item_key in keep:
            keep_qty = keep.get(item_key)
    return keep_qty


def is_scheduler_running():
    return get_env('run_scheduler', False) is True


def get_process_limit():
    return get_env('process_limit', {})


def get_exclude_accounts():
    return get_env('exclude_accounts', [])


def get_trade_config():
    return get_env('trade', {})


def get_vip_trade_config():
    return get_env('trade_vip', get_trade_config())


def get_sleep_configs():
    accounts = get_accounts()
    sleep_configs = {}
    for user_id in accounts:
        account = accounts.get(user_id)
        if 'sleep' in account:
            sleep_configs[user_id] = account.get('sleep')
    return sleep_configs


def get_env(key, default=None):
    try:
        user_data_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), '../.env')
        with open(user_data_path, 'r') as f:
            env = json.load(f)
    except json.JSONDecodeError as e:
        print('ENV JSON格式错误')
        raise e
    except FileNotFoundError:
        env = {}
    return env.get(key, default)

def get_account_status():
    try:
        path = os.path.join(os.path.dirname(os.path.abspath(__file__)), '../data/account_status.json')
        with open(path, 'r') as f:
            status_list = json.load(f)
    except json.JSONDecodeError:
        status_list = {}
    except FileNotFoundError:
        status_list = {}
    return status_list


def save_account_status(data):
    path = os.path.join(os.path.dirname(os.path.abspath(__file__)), '../data/account_status.json')
    status_list_sorted = {k: data[k] for k in sorted(data, key=int)}
    with open(path, 'w') as f:
        json.dump(status_list_sorted, f, indent=4)
